﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 31.03.2008
 * Zeit: 20:07
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */

using System;
using Microsoft.Scripting;
using MSAst = Microsoft.Scripting.Ast;

namespace Gamadus.Engine.Compiler.Ast
{
	public enum AssignSymbol
	{
		Equals,
		AddEquals,
		SubtractEquals,
		MultiplayEquals,
		DivideEquals
	}
	
	/// <summary>
	/// Description of Assignment.
	/// </summary>
	public class Assignment : Statement
	{
		Named m_name;
		AssignSymbol m_symbol;
		Expression m_expr;
		
		public Assignment(Irony.Compiler.AstNode node) : base(node)
		{
			m_name = new Named(node.ChildNodes[0]);
			m_symbol = ParseSymbol(node.ChildNodes[1]);
			m_expr = Expression.MakeExpression(node.ChildNodes[2]);
		}
		
		AssignSymbol ParseSymbol(Irony.Compiler.AstNode node)
		{
			switch((node as Irony.Compiler.Token).Text)
			{
				case ":=":
				case "=":
					return AssignSymbol.Equals;
				case "+=":
					return AssignSymbol.AddEquals;
				case "-=":
					return AssignSymbol.SubtractEquals;
				case "*=":
					return AssignSymbol.MultiplayEquals;
				case "/=":
					return AssignSymbol.DivideEquals;
				default:
					throw new ArgumentException("Invalid assignment symbol");
			}
		}
		
		public override string ToString()
		{
			return "Assignment: "+m_name.ToString()+" ["+m_symbol.ToString()+"] "+m_expr.ToString();
		}
		
		protected internal override MSAst.Expression Generate()
		{
			switch(m_symbol)
			{
				case AssignSymbol.Equals:
					return m_name.GenerateAssign(m_expr.Generate());
				case AssignSymbol.AddEquals:
					return m_name.GenerateAssign(new Binary(m_name, m_expr, Operator.Add).Generate());
				case AssignSymbol.SubtractEquals:
					return m_name.GenerateAssign(new Binary(m_name, m_expr, Operator.Subtract).Generate());
				case AssignSymbol.MultiplayEquals:
					return m_name.GenerateAssign(new Binary(m_name, m_expr, Operator.Multiply).Generate());
				case AssignSymbol.DivideEquals:
					return m_name.GenerateAssign(new Binary(m_name, m_expr, Operator.Divide).Generate());
				default:
					throw new NotImplementedException();
			}
        }
	}
}
